<?php
namespace app\hongyanche\model;
class Collect{
    private $user;
    private $user_id;
    function __construct(){
        $user = $this->user = model('user');
        $user_id = $this->user_id = $user->getUserId();
    }

    //左侧导航
    public function add($data)
    {
        if(!$this->user_id) return array('state'=>0,'errormsg'=>'请登录');
        if(!$data) return ['state'=>0,'errormsg'=>'请选择收藏内容'];
//        if(!$data['url']) return ['state'=>0,'errormsg'=>'收藏失败'];
        //判断是否存在
        $user_id = $this->user_id;
        if($data['forum_id']){
            $forum = db('collect')->where("forum_id = $data[forum_id] and state = 1 and user_id = $user_id")->find();
            if($forum) return ['state'=>0,'errormsg'=>'您已收藏'];
        }
        if($data['mall_id']){
            $mall = db('collect')->where("mall_id = $data[mall_id] and state = 1 and user_id = $user_id")->find();
            if($mall) return ['state'=>0,'errormsg'=>'您已收藏'];
        }
        $data['create_time'] = time();
        $data['state'] = 1;
        $data['user_id'] = $this->user_id;
        $result = db('collect')->insertGetId($data);
        if (!$result) {
            return array('state' => 0, 'errormsg' => '收藏失败');
        }
//        加入action操作
        model('action')->add('collect_create', 'collect_id', $result);
        return array('state' => 1, 'errormsg' => '成功', 'errorcode' => '',);
    }

    //删除收藏
    public function remove($collect_id)
    {
        if(!$this->user_id) return array('state'=>0,'errormsg'=>'请登录');
        if(!$collect_id) return array('state'=>0,'errormsg'=>'请选择一个收藏');
        $res = db('collect')->where("collect_id in($collect_id)")->update(['state'=>0]);
        // 记录action表动作行为
        if ($res) {
            model('action')->add('collect_remove', 'collect_id', $collect_id);
            $ret = array('state' => 1 ,'errormsg'=>'删除成功');
        } else {
            $ret =array('state'=>0,'errormsg'=>'删除失败');
        }
        return $ret;
    }
    //导航栏列表
    function getlist($filter,$class=1){
        if($filter['page']){
            $pages = $filter['page'] - 1;
        }else{
            $pages = 0;
        }
        $maxPerPage = 12;
        if(!$this->user_id) return ['state'=>0,'errormsg'=>'请登录'];
        $list = db('collect')->field('create_time',1)->where("user_id = $this->user_id and state = 1 and class = $class")->limit($maxPerPage,$pages*$maxPerPage)->order('create_time desc')->select();
        if(!$list) return ['state'=>1,'data'=>[]];
        model('product')->demand('mall');
        foreach ($list as $k => $v){
            if($v['mall_id']){
                $mall = db('mall')->where("mall_id = $v[mall_id] and (state = 1 or state = 0)")->find();
                if(!$mall){
                    unset($list[$k]['mall_id']);
                    continue;
                }
                if($mall['state'] == 1){
                    $list[$k]['online'] = true;
                }else{
                    unset($list[$k]['mall_id']);
                    $list[$k]['online'] = false;
                }
                $list[$k]['title'] = $mall['name'];
                $list[$k]['price_min'] = model('product')->getPriceByProductId($v['mall_id'])['price_min'];
                $list[$k]['price_min'] = model('product')->getPriceByProductId($v['mall_id'])['price_max'];
                $list[$k]['imgurl'] = explode(',',$mall['imgurls'])[0];
                $list[$k]['description'] = $mall['description'];
            }elseif($v['forum_id']){
                $forum = db('forum')->where("forum_id = $v[forum_id] and (state = 1 or state = 0)")->find();
                if(!$forum) {
                    unset($list[$k]['forum_id']);
                    continue;
                }
                $list[$k]['title'] = $forum['title'];
                $list[$k]['reply'] = $forum['reply_amount'];
                if($forum['state'] == 1){
                    $list[$k]['online'] = true;
                }else{
                    unset($list[$k]['forum_id']);
                    $list[$k]['online'] = false;
                }
                $user_info = model('user')->getInfoById($forum['user_id']);
                $list[$k]['user_name'] = isset($user_info['set_name']) ? $user_info['set_name'] : $user_info['user_name'];
                //获取用户头像
                $list[$k]['headimgurl'] = db('user')->where("user_id = $forum[user_id]")->find()['headimgurl'];
                $list[$k]['create_time'] = date('Y-m-d',$forum['create_time']);
                $list[$k]['tag'] = $forum['tag'];
                //获取回帖数
            }
        }
        if(!$list) return ['state'=>1,'data'=>[]];
        return ['state'=>1,'data'=>['list'=>$list]];
    }

}